<?
require_once($r."_config/db.php");
require_once($r."_engine/c_sql_table.php");
require_once($r."_inc/c_table_generator2.php");


class CTableEditor
{
	var $table_name, $fields;
	var $id_field = "id";
	var $order = "", $st_handler = "";
	var $page_name;
	var $template_top, $template_bottom;

	var $buttons;
	var $button_add, $button_remove;
	
	var $allow_edit = true;
	
	function Init()
	{
		global $r;
		$template_dir = $r . "_engine/table_editor/";
		$this->template_top		= $template_dir."template_top.php";
		$this->template_bottom	= $template_dir."template_bottom.php";
		
		// Form buttons
		// 't'  -> title,  'ws' -> with selected
		// 'td' -> todo value,  'conf' -> confirmation questions
		$this->button_add = array(
			't'=>"Add new", 'ws'=>0, 'td'=>"add"
		);
		$this->button_remove = array(
			't'=>"Remove selected", 'ws'=>1, 'td'=>"remove",
			'conf'=>"Are you sure you want to delete selected item(s)?"
		);
		$this->buttons = array(
			$this->button_add,
			$this->button_remove
		);
	}
	
	function GetSqlFieldList()
	{
		$sql = "";

		$n = count($this->fields);
		for ($i = 0; $i < $n; $i++)
		{
			$f = $this->fields[$i];
			
			if ($sql != "") $sql .= ", ";
			$sql .= $this->fields[$i]['name'];
		}
		
		return $sql;
	}
	
	
	function PrintTable()
	{
		$tg = new CTableGenerator2(1, true);
		$tg->td_class["default"] = "class=\"textStd\"";

		// Print header (open form, outer table, etc.)
		include $this->template_top;

		// Prepare table header
		$a = array();
		if ($this->allow_edit) $a[] = "";
		for ($i=0, $n=count($this->fields); $i < $n; $i++)
		{
			$a[] = $this->fields[$i]['title'];
		}
		if ($this->allow_edit) $a[] = "Action";
		
		// Print table header
		$tg->PrintHeader();
		for ($i=0, $n=count($a); $i < $n; $i++)
		{
			$t = "header";
			if ($i == 0)	$t .= "|begin";
			if ($i == $n-1)	$t .= "|end";
			$tg->Cell($a[$i], $t);
		}

		// Prepare data
		$query = "SELECT ".$this->GetSqlFieldList()." FROM ".$this->table_name." ".$this->order.";";
		
		$st_handler = $this->st_handler;
		if ($st_handler)
		{
			// Get data from sql table handler
			global $$st_handler;
			$rows = $$st_handler->SelectRows($query);
		}
		else 
		{
			// Use default data access
			$res = execute_query($query);
			$rows = array();
			while ($row = @mysql_fetch_array($res)) $rows[] = $row;
		}

		// Print table body
		foreach ($rows as $row)
		{
			// Prepare data
			$a = array();
			if ($this->allow_edit) $a[] = '<input type="checkbox" name="f_ids[]" value="'.$row[$this->id_field].'">';
			for ($i=0, $n=count($this->fields); $i < $n; $i++)
			{
				$f = $this->fields[$i];
				$v = $row[$f['name']];

				$this->OnPrintData($f, $v);
				
				$a[] = $v;
			}
			if ($this->allow_edit) $a[] = '<a href="'.$this->page_name.'_edit.php?id='.$row[$this->id_field].'">Edit >>></a>';

			// Print data
			for ($i=0, $n=count($a); $i < $n; $i++)
			{
				$t = "";
				if ($i == 0)	$t .= "|begin";
				if ($i == $n-1)	$t .= "|end";
				$tg->Cell($a[$i], $t);
			}
		}

		$tg->PrintFooter();
		
		include $this->template_bottom;
	}
	
	function OnPrintData($f, &$v)
	{
		$this->OnPrintDataStd($f, $v);
	}
	function OnPrintDataStd($f, &$v)
	{
		if (@$f['type'] == "dt")
		{
			$v = date("Y-m-d H:i:s", $v);
		}

		$v = htmlspecialchars($v);
	}
	
	function GetDataForEdit()
	{
		global $id;

		$query = "SELECT ".$this->GetSqlFieldList()." FROM ".$this->table_name." WHERE ".$this->id_field."='".$id."';";
		
		$st_handler = $this->st_handler;
		if ($st_handler)
		{
			global $$st_handler;
			$data = $$st_handler->SelectRow($query);
		}
		else 
		{
			$res = execute_query($query);
			$data = @mysql_fetch_array($res);
		}

		if (!$data) die ("Error: Data not found.");
		
		return $data;
	}
}

?>